package flc.ast.activity;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.ImageUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;

import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.databinding.ActivityIdentifyingBinding;
import flc.ast.utils.Content;
import stark.common.apis.ApiManager;
import stark.common.apis.base.ImgAnimalRet;
import stark.common.apis.base.ImgPlantRet;
import stark.common.apis.base.OcrBankCardRet;
import stark.common.apis.base.OcrBusinessLicenseRet;
import stark.common.apis.base.OcrIdCardRet;
import stark.common.base.IReqRetCallback;

public class IdentifyingActivity extends BaseAc<ActivityIdentifyingBinding> {

    public static String imgPath1, imgPath2;
    private String isCamera = "";
    private int idType;


    @Override
    protected int onCreate() {
        return R.layout.activity_identifying;
    }

    @Override
    protected void initView() {
        mDataBinding.ivIdentifyingBack.setOnClickListener(v -> onBackPressed());

        //身份证识别
        idType = getIntent().getIntExtra(Content.IdCardImg, 0);
        isCamera = getIntent().getStringExtra(Content.IsCamera);

        if (RecCameraActivity.type == 2) {
            if (TextUtils.isEmpty(isCamera)) {
                if (idType == 0) {
                    Glide.with(mContext).load(imgPath1).into(mDataBinding.ivIdentifyingImg);
                } else {
                    Glide.with(mContext).load(imgPath2).into(mDataBinding.ivIdentifyingImg);
                }
            } else {
                Glide.with(mContext).load(imgPath1).into(mDataBinding.ivIdentifyingImg);
            }
        } else {
            Glide.with(mContext).load(imgPath1).into(mDataBinding.ivIdentifyingImg);
        }
    }

    @Override
    protected void initData() {
        Animation animation = new RotateAnimation(0, 359, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        animation.setDuration(1000);
        animation.setRepeatCount(1000);
        animation.setFillAfter(true);//设置为true，动画转化结束后被应用
        mDataBinding.ivIdentifyingTip.startAnimation(animation);//开始动画
        if (RecCameraActivity.type == 0) {
            mDataBinding.tvIdentifyingTitle.setText(getString(R.string.plant_rec));
            plantRec();
        } else if (RecCameraActivity.type == 1) {
            mDataBinding.tvIdentifyingTitle.setText(getString(R.string.animal_rec));
            animalRec();
        } else if (RecCameraActivity.type == 2) {
            mDataBinding.tvIdentifyingTitle.setText(getString(R.string.id_card_rec));
            if (TextUtils.isEmpty(isCamera)) {
                if (idType == 0) {
                    idCardFrontRec();
                } else {
                    idCardReverseRec();
                }
            } else {
                idCardFrontRec();
            }
        } else if (RecCameraActivity.type == 3) {
            bankCardRec();
        } else if (RecCameraActivity.type == 4) {
            businessLicenseRec();
        }
    }

    private void plantRec() {
        ApiManager.imgApi().identifyPlant(IdentifyingActivity.this, ImageUtils.getBitmap(imgPath1), new IReqRetCallback<List<ImgPlantRet>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<ImgPlantRet> result) {
                if (success) {
                    if (result != null && result.size() != 0) {
                        if (result.get(0).name.equals(getString(R.string.non_plant_name))) {
                            ToastUtils.showShort(R.string.no_find_data);
                        } else {
                            PlantRecActivity.imgPath = imgPath1;
                            PlantRecActivity.sPlantRets = result;
                            startActivity(PlantRecActivity.class);
                        }
                    } else {
                        ToastUtils.showShort(R.string.no_find_data);
                    }
                } else {
                    ToastUtils.showShort(R.string.no_find_data);
                }
                finish();
            }
        });
    }

    private void animalRec() {
        ApiManager.imgApi().identifyAnimal(IdentifyingActivity.this, ImageUtils.getBitmap(imgPath1), new IReqRetCallback<List<ImgAnimalRet>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<ImgAnimalRet> result) {
                dismissDialog();
                if (success) {
                    if (result != null && result.size() != 0) {
                        if (result.get(0).name.equals(getString(R.string.non_animal_name))) {
                            ToastUtils.showShort(R.string.no_find_data);
                        } else {
                            AnimalRecActivity.imgPath = imgPath1;
                            AnimalRecActivity.sAnimRets = result;
                            startActivity(AnimalRecActivity.class);
                        }
                    } else {
                        ToastUtils.showShort(R.string.no_find_data);
                        startActivity(IdCardRecActivity.class);
                    }
                } else {
                    ToastUtils.showShort(R.string.no_find_data);
                    startActivity(IdCardRecActivity.class);
                }
                finish();
            }
        });
    }

    private void idCardFrontRec() {
        ApiManager.ocrApi().identifyIdCard(IdentifyingActivity.this, ImageUtils.getBitmap(imgPath1), new IReqRetCallback<OcrIdCardRet>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable OcrIdCardRet result) {
                if (success) {
                    if (result != null) {
                        if (result.idcard_number_type == -1) {
                            ToastUtils.showShort(R.string.id_empty_tips);
                            startActivity(IdCardRecActivity.class);
                        } else if (result.idcard_number_type == 0) {
                            ToastUtils.showShort(R.string.id_illegal_tips);
                            startActivity(IdCardRecActivity.class);
                        } else if (result.idcard_number_type == 2) {
                            ToastUtils.showShort(R.string.gender_birth_no_equals_tips);
                            startActivity(IdCardRecActivity.class);
                        } else if (result.idcard_number_type == 3) {
                            ToastUtils.showShort(R.string.id_birth_no_equals_tips);
                            startActivity(IdCardRecActivity.class);
                        } else if (result.idcard_number_type == 4) {
                            ToastUtils.showShort(R.string.id_gender_no_equals_tips);
                            startActivity(IdCardRecActivity.class);
                        } else {
                            IdCardRecActivity.sIdCardRetFront = result;
                            if (TextUtils.isEmpty(isCamera)) {
                                Intent intent = new Intent();
                                intent.putExtra(Content.FrontImg, imgPath1);
                                intent.putExtra(Content.ReverseImg, "");
                                setResult(RESULT_OK, intent);
                                finish();
                            } else {
                                idCardReverseRec();
                            }
                        }
                    } else {
                        ToastUtils.showShort(R.string.no_find_data);
                        startActivity(IdCardRecActivity.class);
                    }
                } else {
                    ToastUtils.showShort(R.string.no_find_data);
                    startActivity(IdCardRecActivity.class);
                }
            }
        });
    }

    private void idCardReverseRec() {
        ApiManager.ocrApi().identifyIdCard(IdentifyingActivity.this, ImageUtils.getBitmap(imgPath2), new IReqRetCallback<OcrIdCardRet>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable OcrIdCardRet result) {
                if (success) {
                    if (result != null) {
//                        if (result.idcard_number_type == -1) {
//                            ToastUtils.showShort(R.string.id_empty_tips);
//                            startActivity(IdCardRecActivity.class);
//                        } else if (result.idcard_number_type == 0) {
//                            ToastUtils.showShort(R.string.id_illegal_tips);
//                            startActivity(IdCardRecActivity.class);
//                        } else if (result.idcard_number_type == 2) {
//                            ToastUtils.showShort(R.string.gender_birth_no_equals_tips);
//                            startActivity(IdCardRecActivity.class);
//                        } else if (result.idcard_number_type == 3) {
//                            ToastUtils.showShort(R.string.id_birth_no_equals_tips);
//                            startActivity(IdCardRecActivity.class);
//                        } else if (result.idcard_number_type == 4) {
//                            ToastUtils.showShort(R.string.id_gender_no_equals_tips);
//                            startActivity(IdCardRecActivity.class);
//                        } else {
                        Intent intent = new Intent();
                        if (TextUtils.isEmpty(isCamera)) {
                            intent.putExtra(Content.FrontImg, "");
                            intent.putExtra(Content.ReverseImg, imgPath2);
                        } else {
                            intent.putExtra(Content.FrontImg, imgPath1);
                            intent.putExtra(Content.ReverseImg, imgPath2);
                        }
                        IdCardRecActivity.sIdCardRetReverse = result;
                        setResult(RESULT_OK, intent);
                        finish();
//                        }
                    } else {
                        ToastUtils.showShort(R.string.no_find_data);
                        startActivity(IdCardRecActivity.class);
                    }
                } else {
                    ToastUtils.showShort(R.string.no_find_data);
                    startActivity(IdCardRecActivity.class);
                }
            }
        });
    }

    private void bankCardRec() {
        ApiManager.ocrApi().identifyBankCard(this, ImageUtils.getBitmap(imgPath1), new IReqRetCallback<OcrBankCardRet>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable OcrBankCardRet result) {
                if (success) {
                    if (result != null) {
                        BankCardActivity.sOcrBankCardRet = result;
                        Intent intent = new Intent();
                        intent.putExtra(Content.RecognizeImg, imgPath1);
                        setResult(RESULT_OK, intent);
                        finish();
                    } else {
                        ToastUtils.showShort(R.string.no_find_data);
                        startActivity(BankCardActivity.class);
                    }
                } else {
                    ToastUtils.showShort(R.string.no_find_data);
                    startActivity(BankCardActivity.class);
                }
            }
        });
    }

    private void businessLicenseRec() {
        ApiManager.ocrApi().identifyBusinessLicense(this, ImageUtils.getBitmap(imgPath1), new IReqRetCallback<OcrBusinessLicenseRet>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable OcrBusinessLicenseRet result) {
                if (success) {
                    if (result != null) {
                        BusinessLicenseRecActivity.sOcrBusinessLicenseRet = result;
                        Intent intent = new Intent();
                        intent.putExtra(Content.RecognizeImg, imgPath1);
                        setResult(RESULT_OK, intent);
                        finish();
                    } else {
                        ToastUtils.showShort(R.string.no_find_data);
                        startActivity(BusinessLicenseRecActivity.class);
                    }
                } else {
                    ToastUtils.showShort(R.string.no_find_data);
                    startActivity(BusinessLicenseRecActivity.class);
                }
            }
        });
    }

}